package com.feidian.helper.ui;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.feidian.helper.R;
import com.feidian.helper.bean.Table;
import com.feidian.helper.bean.User;
import com.feidian.helper.net.NetManager;

import eidian.sail.entity.Listener.ImageLoadListener;
import eidian.sail.entity.Listener.LoginListener;
import eidian.sail.entity.Student;
import eidian.sail.helper.HttpHelper;

public class RegisterActivity extends Activity implements OnClickListener,
		ImageLoadListener, LoginListener {

	private static final int IMAGE_LOAD_SUCCESS = 0X111;
	private static final int IMAGE_LOAD_FAILURE = 0X112;
	private static final int LOGIN_SUCCESS = 0X113;
	private  static final int LOGIN_FAILURE = 0X114;
	private static final int GET_CONTENT=0X115;
	private TextView title;

	private TextView back;
	
	private EditText mUserCountEdit;
	private EditText mPasswordEdit;
	private EditText mCheckCodeEdit;
	private Button mRegisterBtn;
	private ImageView mCheckCodeIV;
	private Bitmap mBitmap;
	private HttpHelper mHttpHelper;
	private Student mStudent;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.activity_register);
		initView();
		getImage();
	}

	private void initView() {
		mUserCountEdit = (EditText) findViewById(R.id.username_edit);
		mPasswordEdit = (EditText) findViewById(R.id.userpassword_edit);
		mCheckCodeEdit = (EditText) findViewById(R.id.user_code);
		mRegisterBtn = (Button) findViewById(R.id.register_button);
		mCheckCodeIV = (ImageView) findViewById(R.id.checkcode_image);
		mCheckCodeIV.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				getImage();
			}
		});
		mRegisterBtn.setOnClickListener(this);
		mHttpHelper=new HttpHelper(this);
		
	}

	private void getImage() {
		mHttpHelper.getCheckImage(this);
	}

	@Override
	public void imageLoadSuccessBack(Bitmap bitmap) {
		this.mBitmap = bitmap;
		Message msg = mHandler.obtainMessage();
		msg.what = IMAGE_LOAD_SUCCESS;
		mHandler.sendMessage(msg);

	}

	@Override
	public void imageLoadFailBack(String error) {
		Message msg =mHandler.obtainMessage();
		msg.what = IMAGE_LOAD_FAILURE;
		mHandler.sendMessage(msg);

	}

	@Override
	public void onSuccessBack(Student student) {
		mStudent = student;
		Message msg =mHandler.obtainMessage();
		msg.what = LOGIN_SUCCESS;
		mHandler.sendMessage(msg);

	}

	@Override
	public void onFailBack(String error) {
		Log.e("login_failure",".....");
		mHttpHelper.getCheckImage(RegisterActivity.this);
		Message msg = mHandler.obtainMessage();
		msg.what = LOGIN_FAILURE;
		mHandler.sendMessage(msg);
	}
	
	 @Override
	 public void onClick(View arg0) {

		 try {	
			 mHttpHelper.login(this
					 , new Student(mUserCountEdit.getText().toString().trim(),mPasswordEdit.getText().toString().trim())
			 		 ,mCheckCodeEdit.getText().toString().trim());
			 Log.e("onClick",".....");
		 } catch (NumberFormatException e) {
			 e.printStackTrace();
		 } catch (Exception e) {
			 e.printStackTrace();
		 }
	 }
	
	private Handler mHandler = new Handler() {

		public void handleMessage(Message msg) {
			switch (msg.what) {
			case IMAGE_LOAD_SUCCESS:
				
				mCheckCodeIV.setImageBitmap(mBitmap);
				break;
			case LOGIN_SUCCESS:
				User.stuId=mStudent.getID();
				User.name=mStudent.getName();
				User.password=mStudent.getPswd();
				Log.e("个人信息:",
						"姓名" + mStudent.getName() + ",学号：" + mStudent.getID()
								+ ",密码：" + mStudent.getPswd());
				String url="http://115.29.103.105/User/Register";
				List<NameValuePair> params=new ArrayList<NameValuePair>();
				params.add(new BasicNameValuePair(Table.stuId,mStudent.getID()));
				params.add(new BasicNameValuePair(Table.name,mStudent.getName()));
				params.add(new BasicNameValuePair(Table.password,mStudent.getPswd()));
				params.add(new BasicNameValuePair(Table.sex,"0"));
				RegisterThread thread=new RegisterThread(url, params);
				thread.start();
				break;
			case IMAGE_LOAD_FAILURE:
				Toast.makeText(RegisterActivity.this, "获取验证码失败",
						Toast.LENGTH_SHORT).show();
				break;
			case LOGIN_FAILURE:
				Toast.makeText(RegisterActivity.this, "登录失败",
						Toast.LENGTH_SHORT).show();
				break;
			case GET_CONTENT:
				Log.e("getContext=",(String)msg.obj);
				//Toast.makeText(RegisterActivity.this,"getContext="+(String)msg.obj , Toast.LENGTH_LONG).show();
				Dialog mDialog=new Dialog(RegisterActivity.this,R.style.dialog);
				View view=getLayoutInflater().inflate(R.layout.remember_count, null,false);
				TextView tv=(TextView)view.findViewById(R.id.content);
				tv.setText("你的账号是:"+mStudent.getID()+"\n你的密码是:"+mStudent.getPswd());
				Button bn=(Button)view.findViewById(R.id.sure);
				bn.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View arg0) {
						// TODO Auto-generated method stub
						Intent intent =new Intent(RegisterActivity.this,MainFragmentActivity.class);
						startActivity(intent);
					}
				});
				mDialog.setContentView(view);
				mDialog.show();
				break;
			}
		};
	};
	class RegisterThread extends Thread
	{
		private String mUrl;
		private List<NameValuePair> mParams;
		public RegisterThread(String url,List<NameValuePair> params) {
			// TODO Auto-generated constructor stub
			mUrl=url;
			mParams=params;
		}
		@Override
		public void run() {
			// TODO Auto-generated method stub
			String context=NetManager.getNetManager().getWebWithPost(mUrl, mParams);
			if(context!=null)
			Log.e("contenxt=",context);
			else Log.e("context=","null");
			Message msg=mHandler.obtainMessage();
			msg.obj=context;
			msg.what=GET_CONTENT;
			mHandler.sendMessage(msg);
			super.run();
		}
	}

}
